﻿<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<List<FileInfo>>" %>
<%@ Import Namespace="System.IO" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Upload Images/Files</title>
    <script type="text/javascript" src="/Content/ckeditor/ckeditor.js"></script>
    <script src="../../Scripts/jquery-1.6.2.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        var totalPage = <%=ViewData["numPage"]%>;
        var pageNum = <%=WebHoiChoMuaBan.Controllers.Global.Config.PAGE_NUMBER_VISIABLE %>;
        
        function GetIndex(str){
            var index = str.substring(2, str.length);
            return index;
        }

        function Preview() {
            if ($('#aNext').css('visibility') == "hidden")
                $('#aNext').css('visibility', 'visible');
            var decreaseValue = 0;
            for (var i = 1; i <= pageNum; i++) {
                var index = GetIndex(document.getElementById("pageIndex" + i).text);
                if (index > 1){
                    if (i == 1){
                        decreaseValue = (index > pageNum) ? pageNum : (index - 1);
                    }
                    var newValue = parseInt(index, 10) - decreaseValue;
                    document.getElementById("pageIndex" + i).innerHTML = " | " + newValue.toString();
                    if (newValue == 1)
                        $('#aPreview').css('visibility', 'hidden');
                }
                else
                    break;
            }
        }

        function Next() {
            if ($('#aPreview').css('visibility') == "hidden")
                $('#aPreview').css('visibility', 'visible');
            var increaseValue = 0;
            for (var i = pageNum; i >= 1; i--) {
                var index = GetIndex(document.getElementById("pageIndex" + i).text);
                if (index < totalPage){
                    if (i == pageNum){
                        increaseValue = totalPage - index;
                        increaseValue = (increaseValue >= pageNum) ? pageNum : increaseValue;
                    }
                    var newValue = parseInt(index, 10) + increaseValue;
                    document.getElementById("pageIndex" + i).innerHTML = " | " + newValue.toString();
                    if (newValue == totalPage)
                        $('#aNext').css('visibility', 'hidden');
                }
                else
                    break;
            }
        }
    </script>
</head>
<body>           
    <form action="<%= Url.Action("/Index","Upload") %>" method="post" enctype="multipart/form-data">  
      <label for="file">Image filename:</label><br />
      <input type="file" name="file" id="file" /><br />

      <input id="CKEditorFuncNum" name="CKEditorFuncNum" type="hidden" 
        value="<%= ViewData["CKEditorFuncNum"].ToString() %>" />
      <input id="CKEditor" name="CKEditor" type="hidden" 
        value="<%= ViewData["CKEditor"].ToString() %>" />
      <input id="langCode" name="langCode" type="hidden" 
        value="<%= ViewData["langCode"].ToString() %>" />

      <input type="submit" value="Upload" />
    </form>

    <hr />

    <div>
        <a id="aPreview" href="#" onclick="Preview()" style="visibility:hidden; text-decoration:none;"><<</a>
        <%
            for (int i = 1; i <= WebHoiChoMuaBan.Controllers.Global.Config.PAGE_NUMBER_VISIABLE && i <= int.Parse(ViewData["numPage"].ToString()); i++)
			{
		%>
        <%= Html.ActionLink(" | " + i.ToString(), "/Index", new { CKEditorFuncNum = ViewData["CKEditorFuncNum"].ToString(), 
                                                                CKEditor = ViewData["CKEditor"].ToString(),
                                                                langCode = ViewData["langCode"].ToString(),
                                                                page = i },
                                                            new { id = ("pageIndex" + i), 
                                                                style = "text-decoration:none;" })
        %>
        <%
			}
        %>
        <a id="aNext" href="#" onclick="Next()" style="text-decoration:none;"> | >></a>
    </div>
    
    <table cellspacing="10px">
        <%  if (Model.Count > 0)
            {
                string CKEditorUrl = "";
                string imageUrl = "";
                int i = 0;
                foreach (FileInfo image in Model)
                {
                    if (i % WebHoiChoMuaBan.Controllers.Global.Config.COLS_UPLOAD == 0)
                        Response.Write("<tr>");
                    CKEditorUrl = "/Content/Images/Upload/" + image.Name;
                    imageUrl = "../Content/Images/Upload/" + image.Name;
        %>
                    <td>
                        <a title="<%= image.Name %>" href="javascript:void(0);" onclick="window.opener.CKEDITOR.tools.callFunction(<%= ViewData["CKEditorFuncNum"] %>, '<%= CKEditorUrl %>', ''); window.close();">
                            <img src="<%= imageUrl %>" border="0" alt="<%= image.Name %>" width="100" height="100" />
                        </a>
                    </td>     
        <%  
                    if (i++ % WebHoiChoMuaBan.Controllers.Global.Config.COLS_UPLOAD == 7)
                    {
                        Response.Write("</tr>");
                        //if (i >= WebHoiChoMuaBan.Controllers.Global.Config.ROWS_UPLOAD * WebHoiChoMuaBan.Controllers.Global.Config.COLS_UPLOAD)
                        //    break;
                    }
                }
           } %>
    </table>
</body>
</html>